package com.group3.admin.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DbUtils {
	private static String driverClassName = "oracle.jdbc.OracleDriver";
	private static String url = "jdbc:oracle:thin:@zha-ita034-w7:1521:xe";
	private static String user = "joel";
	private static String password = "joel";
	static {
		try {
			Class.forName(driverClassName);
//			Context initContext;
//			try {
////				initContext = new InitialContext();
////				Context envContext = (Context) initContext.lookup("java:/comp/env");
////				DataSource ds = (DataSource)envContext.lookup("jdbc/joel_dbcp_connect");
//				Connection conn = ds.getConnection();
//			} catch (NamingException | SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection (){
		Connection conn = null;
		try {
			conn =  DriverManager.getConnection(url, user, password);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void free(Connection conn,PreparedStatement ps,ResultSet rs) {
		
		try {
			if(rs != null){
			rs.close();
			}
			if(ps != null) {
			ps.close();}
			if(conn != null){
 			conn.close();}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
